package com.mango.ch04;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

public class MyPair implements WritableComparable<MyPair> {
	private String first = new String();
	private String second = new String();

	public MyPair(String first, String second) {
		super();
		this.first = first;
		this.second = second;
	}

	public MyPair() {
	}

	@Override
	public void write(DataOutput out) throws IOException {
		// TODO Auto-generated method stub
		out.writeUTF(first);
		out.writeUTF(second);
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		// TODO Auto-generated method stub
		this.first = in.readUTF();
		this.second = in.readUTF();
	}

	@Override
	public String toString() {
		return "MyPair [first=" + first + ", second=" + second + "]";
	}

	public void set(String first, String second) {
		this.first = first;
		this.second = second;
	}

	public String getSecond() {
		return second;
	}

	public String getFirst() {
		return first;
	}

	@Override
	public int compareTo(MyPair o) {
		// TODO Auto-generated method stub
		return this.first.compareTo(o.getFirst());
	}

	@Override
	public int hashCode() {
		return first.hashCode() + second.hashCode();
	}

	@Override
	public boolean equals(Object right) {
		if (right instanceof MyPair) {
			MyPair r = (MyPair) right;
			return r.first == first && r.second == second;
		} else {
			return false;
		}
	}
}
